package  
{
	import fl.controls.Button;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import zdc.Util;
	
	/**
	 * ...
	 * @author Metal Slime
	 */
	public class SocketTester extends MovieClip 
	{
		private var _sock:Socket;
		private var _connBtn:Button;
		private var _sendBtn:Button;
		private var _msgInput:TextInput;
		private var _portInput:TextInput;
		private var _hostInput:TextInput;
		private var _msgs:TextArea;
		private var _port:int;
		private var _host:String;
		
		public function SocketTester() 
		{
			stage? init() : addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			initVars();
			initState();
			initEvents();
			run();
		}
		
		private function initVars():void 
		{
			_connBtn = this.connBtn;
			_sendBtn = this.sendBtn;
			_msgInput = this.msgInput;
			_portInput = this.portInput;
			_hostInput = this.hostInput;
			_msgs = this.msgs;
			
			_sock = new Socket();
			_sock.addEventListener(ProgressEvent.SOCKET_DATA, onReceived);
			_sock.addEventListener(Event.CLOSE, onClose);
			_sock.addEventListener(Event.CONNECT, onConnect);
			_sock.addEventListener(IOErrorEvent.IO_ERROR, onErr);
			_sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onErr);
			
			_sendBtn.enabled = false;
			_sendBtn.addEventListener(MouseEvent.CLICK, onSendClick);
			_connBtn.addEventListener(MouseEvent.CLICK, onConnClick);
		}
		private var _id:int = 1;
		//发送
		private function onSendClick(e:MouseEvent):void 
		{
			if (_sock.connected) {
				if (!Util.isEmpty(_msgInput)) {
					var cont:String;
					cont = JSON.stringify( { id: _id, arg: _msgInput.text, arr: [1, 2, 3, 4, 5] } );
					_id = _id == 1? 2 : 1;
					addMsg("发送信息：" + cont);
					//_sock.writeUTFBytes(cont);
					_sock.writeUTFBytes(_msgInput.text);
					_sock.flush();
				}
			}
			else {
				addMsg("连接已关闭，请重新打开连接！");
			}
		}
		
		//连接
		private function onConnClick(e:MouseEvent):void 
		{
			if (_sock.connected) {
				_sock.close();
				addMsg("关闭连接！");
				_connBtn.label = "连接";
			}
			else{
				_hostInput.text = Util.trim(_hostInput.text);
				_portInput.text = Util.trim(_portInput.text);
				if (!Util.isEmpty(_hostInput.text) && Util.isInt(_portInput.text, true, 4)) {
					addMsg("尝试连接服务器" + _hostInput.text + ":" + _portInput.text);
					_sock.connect(_hostInput.text, int(_portInput.text));
					_connBtn.label = "断开";
				}
			}
		}
		
		private function onReceived(e:ProgressEvent):void 
		{
			try {
				var cont:String = _sock.readUTFBytes(_sock.bytesAvailable);
				addMsg("接收到信息：" + JSON.parse(cont));
			}
			catch (e:Error) {	 
				addMsg(e);
			}
		}
		
		private function onClose(e:Event):void 
		{
			_sendBtn.enabled = false;
			_connBtn.label = "连接";
			addMsg("连接已关闭");
		}
		
		private function onConnect(e:Event):void 
		{
			_sendBtn.enabled = true;
			addMsg("已连接上服务器");
		}
		
		
		private function onErr(e:Event):void 
		{
			addMsg(e);
		}
		
		private function initState():void 
		{
			
		}
		
		private function initEvents():void 
		{
			
		}
		
		private function run():void 
		{
			
		}
		
		private function addMsg(msg:*):void {
			_msgs.appendText(msg + "\n");
		}
		
	}
}